package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.AllocationApplyBean;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.IAllocationApplyView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AllocationApplyPresenter extends BasePresenter<IAllocationApplyView> {

    public AllocationApplyPresenter(IAllocationApplyView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    public void getAllocationList(int page) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("out_warehouse_id", "0");//调出仓库id 默认是零
        params.put("limit", "20");
        params.put("page", page + "");
        params.put("apply_status", "2");
//        params.put("bill_status", "0");
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class).getAllocationApplyList(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<AllocationApplyBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<AllocationApplyBean> result) {
                        if (result.isSuccess()) {
                            if (null != result.getData()) {
                                view.loadSuccess(result.getData());
                            } else {
                                view.loadFailed("暂无数据");
                            }
                        } else {
                            view.loadFailed("请求失败，请稍后重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.loadFailed("请求异常了");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getAllocationList(int page, String startDate, String endDate, String wareHousId, String emplyeeId, String status) {

        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("limit", "20");
        params.put("page", page + "");
        params.put("employee_id", emplyeeId);
        params.put("start_time", startDate);
        params.put("apply_status", "2");
        params.put("end_time", endDate);
        params.put("out_warehouse_id", wareHousId);//调出仓库id 默认是零
        params.put("bill_status", status);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        RetrofitManager.getDefault().create(ApiServer.class).getAllocationApplyList(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<AllocationApplyBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<AllocationApplyBean> result) {
                        if (result.isSuccess()) {
                            if (null != result.getData()) {
                                view.loadSuccess(result.getData());
                            } else {
                                view.loadFailed("暂无数据");
                            }
                        } else {
                            view.loadFailed("请求失败，请稍后重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.loadFailed("请求异常了");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
